package solution.T0215_1_Clipboard;

import solution.T0215_1_Clipboard.command.CopyCommand;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// CopyHistory 维护一个 Map<Integer, CopyCommand>
class CopyHistory {
    private final Map<Integer, CopyCommand> history = new HashMap<>();

    public void add(CopyCommand command) {
        history.put(command.getId(), command);
    }

    public CopyCommand getById(int id) {
        return history.get(id);
    }

    /**
     *
     * @param keyword 当 keyword 为 null 的时候，获取完整复制历史。
     * @return
     */
    public List<CopyCommand> getHistory(String keyword) {
        List<CopyCommand> result = new ArrayList<>();
        for (CopyCommand command : history.values()) {
            if (keyword == null || command.getContent().toLowerCase().contains(keyword.toLowerCase())) {
                result.add(command);
            }
        }
        return result;
    }

    public void clear() {
        history.clear();
    }
}
